<template>
  <div v-loading.lock="loading">
    <div class="query">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item :label="dataList.UserId">
          <el-input v-model="queryForm.userId" :placeholder="dataList.UserId" @keyup.enter.native="onQuery" />
        </el-form-item>
        <el-form-item :label="dataList.RoleId">
          <el-input v-model="queryForm.roleId" :placeholder="dataList.RoleId" @keyup.enter.native="onQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
          <el-button type="primary" @click="onNew">{{ btn.Add }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container">
      <el-table
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{textAlign: 'center' }"
      >
        <el-table-column align="center" :label="dataList.number" width="50">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column :label="dataList.UserId">
          <template slot-scope="scope">{{ scope.row.userId }}</template>
        </el-table-column>
        <el-table-column :label="dataList.RoleId">
          <template slot-scope="scope">{{ scope.row.roleId }}</template>
        </el-table-column>
        <el-table-column :label="btn.operate">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">{{ btn.Edit }}</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">{{ btn.Delete }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          :current-page="pagination.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>

    <PermissionUserRoleEdit
      ref="PermissionUserRoleEdit"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>

<script>
import { deleteRecord, getList } from "../../../api/Permission/PermissionUserRole.js";
import PermissionUserRoleEdit from "./PermissionUserRoleEdit.vue";

export default {
  name: "PermissionUserRoleList",
  components: { PermissionUserRoleEdit },
  data() {
    return {
      loading: true,
      list: null,
      queryForm: {
        userId: 0,
        roleId: 0,
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("Permission.buttons");
    },
    dataList() {
      return this.$t("Permission.PermissionUser");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    handleCurrentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    handleSizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList;
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.PermissionUserRoleEdit.formData = this.Helper.deepCopy(
        this.$refs.PermissionUserRoleEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.PermissionUserRoleEdit.formData = rowData;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
